import time
from autoframe.common.Public_method_common import Browser, By
import unittest
from selenium import webdriver


class prepare(Browser,unittest.TestCase):
    def test1_Sign_in(self):
        self.driver.implicitly_wait(10)

        # 进入浏览器
        self.browser('chrome')
        self.address('http://172.16.140.57/ecshop/user.php')
        # 登录
        self.location((By.NAME, 'username')).send_keys('yy123456')
        self.location((By.NAME, 'password')).send_keys('123456')
        time.sleep(2)
        self.location((By.ID, 'remember')).click()
        self.location((By.NAME, 'submit')).click()
        # a = self.location((By.LINK_TEXT,'首页')).text
        self.assertEqual(1,1)

        # 进入购物车页面

    def test2_Shopping_Cart(self):
        self.driver.implicitly_wait(10)
        time.sleep(2)
        self.location((By.LINK_TEXT, '首页')).click()  # 进入首页
        # 滑动找到商品加入购物车
        time.sleep(2)
        self.location((By.XPATH, '/html/body/div[6]/div/div/div[2]/div[2]/div/a[1]/div[1]')).click()
        time.sleep(2)
        self.location((By.CSS_SELECTOR, '#ECS_FORMBUY > ul > li.padd > table > tbody > tr > td.td1 > a > img')).click()
        b = self.driver.current_window_handle
        self.assertEqual(b,'http://172.16.140.57/ecshop/flow.php?step=cart',msg='进入失败')

        # 改
    def test3_change(self,num='3'):
        self.driver.implicitly_wait(10)
        time.sleep(5)
        cookie = self.location((By.CLASS_NAME, "inputBg"))
        # print(cookie.get_attribute('outerHTML'))
        cookie.clear()
        cookie.send_keys(num)
        c = self.location((By.CLASS_NAME,'inputBg')).text
        self.assertTrue(c,msg='修改失败')

    # 页面检查
    def test4_UI(self):
        time.sleep(2)
        self.driver.implicitly_wait(10)
        self.location((By.LINK_TEXT, '首页'))
        # 点击商品标
        time.sleep(2)
        self.location((By.XPATH, '//*[@id="ECS_CARTINFO"]/a')).click()
        # 继续购物
        time.sleep(2)
        self.location((By.CSS_SELECTOR,'body > div:nth-child(7) > div.flowBox > table > tbody > tr > td:nth-child(1) > a > img')).click()
        # 更新购物车
        self.location((By.XPATH, '//*[@id="ECS_CARTINFO"]/a')).click()
        self.location((By.XPATH, '//input[@class="bnt_blue_1"][2]')).click()
        # 结算
        self.location((By.XPATH, '//*[@id="ECS_CARTINFO"]/a')).click()
        self.location((By.CSS_SELECTOR,'body > div:nth-child(7) > div.flowBox > table > tbody > tr > td:nth-child(2) > a > img')).click()
        self.location((By.XPATH, '//*[@id="ECS_CARTINFO"]/a')).click()
        # 清空购物车
        self.location((By.XPATH, '//input[@class="bnt_blue_1"][1]')).click()
        d = self.location((By.LINK_TEXT, '首页')).text
        self.assertEqual(d, '首页', msg='登录失败')
    # 删除
    def test5_delete(self):
        self.location((By.XPATH, '/html/body/div[6]/div/div/div[2]/div[2]/div/a[1]/div[1]')).click()
        self.location((By.CSS_SELECTOR, '#ECS_FORMBUY > ul > li.padd > table > tbody > tr > td.td1 > a > img')).click()
        time.sleep(5)
        QQ = self.location((By.XPATH,'/html/body/div[6]/div[1]/form/table[1]/tbody/tr[2]/td[7]/a[1]'))
        QQ.click()
        self.driver.switch_to.alert.accept()
        e=self.location((By.LINK_TEXT,'智能相机')).text
        self.assertNotEqual(e, '智能相机',msg='删除失败')

    def test6_quit(self):
        self.driver.quit()
        f = self.location((By.LINK_TEXT,'首页')).text
        self.assertNotEqual(f,'首页',msg='退出失败')


if __name__ == '__main__':
    csae=prepare()
    csae.test1_Sign_in()
    csae.test2_Shopping_Cart()
    csae.test3_change()
    csae.test4_UI()
    csae.test5_delete()
    csae.test6_quit()

    # # csae.delete()

    # unittest.main(verbosity=2)

'''
class Enter_browser:

    def enter(self):
        self.driver = webdriver.Chrome()
        self.driver.get("http://172.16.140.57/ecshop/user.php")
        self.driver.find_element(By.NAME, 'username').send_keys('yy123456')
        self.driver.find_element(By.NAME, 'password').send_keys('123456')
        self.driver.find_element(By.ID, 'remember').click()
        self.driver.find_element(By.NAME, 'submit').click()
    def Shopping_Cart(self):
        self.driver.find_element(By.LINK_TEXT, '首页').click()
        self.driver.find_element(By.XPATH, '/html/body/div[6]/div/div/div[2]/div[2]/div/a[1]').click()
        self.driver.find_element(By.XPATH, '//*[@id="ECS_FORMBUY"]/ul/li[7]/table/tbody/tr/td[1]/a/img').click()
        # self.driver.find_element(By.XPATH,'//*[@id="formCart"]/table[1]/tbody/tr[2]/td[1]/a[1]/img').click()
        self.driver.find_element(By.XPATH,'//*[@id="formCart"]/table[1]/tbody/tr[2]/td[7]/a[1]').click()
        # self.driver.find_element(By.XPATH,'//*[@id="ECS_FORMBUY"]/ul/li[7]/table/tbody/tr/td[1]/a/img').click()
        self.driver.find_element(By.XPATH,'/html/body/div[6]/div[1]/table/tbody/tr/td[1]/a/img').click()

if __name__ == '__main__':
    csae =Enter_browser()
    csae.enter()
    csae.Shopping_Cart()

'''
